<?php

namespace App\Http\Controllers\Web;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Chat;
use App\Models\ChatRoom;
use App\Traits\BaseFun;
use App\Repositories\UserRepository;
class IndexController extends Controller
{
    //
    public function __construct(UserRepository $UserRepository)
    {
        $this->UserRepository=$UserRepository;
    }

    //111
    public function index(Request $request){
        // return view('web.index');
        $userList=$this->UserRepository->indexlist();
        $userinfo=$request->session()->get('userinfo');
        return view('web.chat_list',['userinfo'=>$userinfo,'userList'=>$userList]);
    }
    public function login(Request $request){
        return view('web.login');
    }
    public function register(Request $request){
        return view('web.register');
    }
    public function chat_list(Request $request){
        $userList=$this->UserRepository->indexlist();
        $userinfo=$request->session()->get('userinfo');
        return view('web.chat_list',['userinfo'=>$userinfo,'userList'=>$userList]);
    }
    //退出登陆
    public function login_out(Request $request){
        $request->session()->forget('userinfo');
        return redirect('login');
    }
    //登陆操作
    public function ajax_login(Request $request){
        $userInfo=User::where('account',$request->post('account'))->where('password',md5($request->post('password')))->first();
        if($userInfo){
            session(['userinfo'=>$userInfo]);
            return BaseFun::jsonWebReturn(['code'=>200,'data'=>$userInfo,'msg'=>'登陆成功']);
        }else{
            return BaseFun::jsonWebReturn(['code'=>202,'msg'=>'账号或密码错误']);
        }
    }
    //注册操作
    public function ajax_register(Request $request){
        $userInfo=User::where('account',$request->post('account'))->where('password',md5($request->post('password')))->first();
        if($userInfo){
            return BaseFun::jsonWebReturn(['code'=>201,'msg'=>'账号已被注册']);
        }else{
            $data=$request->input();
            $data['avatar']="/upload/avatar/".mt_rand(1,15).".jpg";
            $res = $this->UserRepository->create($data);
            session(['userinfo'=>$res]);
            if($res){
                return BaseFun::jsonWebReturn(['code'=>200,'data'=>$userInfo,'msg'=>'注册成功']);
            }
            return BaseFun::jsonWebReturn(['code'=>202,'msg'=>'账号或密码错误']);
        }
    }





    //生成二维码https://laravelacademy.org/post/2605.html
    public function qrcode(){
        $url=$_GET['url'];
        $data = BaseFun::qrcode($url);
        echo $data;
    }

    public function upload_images(Request $request)//上传图片,多图上传
	{
		$file = $_FILES["file"];
		if ($file==null) {
			exit(json_encode(array('code'=>1, 'msg'=>'未上传图片')));
		}
		// 获取文件后缀
		$temp = explode(".", $_FILES["file"]["name"]);
		$extension = end($temp);
		// 判断文件是否合法
		if(!in_array($extension, array("gif","jpeg","jpg","png"))){
			exit(json_encode(array('code'=>1, 'msg'=>'上传图片不合法')));
		}
		//创建文件夹获取当前年月日
		$folder="/upload/images/".date('Y-m-d');

		if(!is_dir('.'.$folder)){
			mkdir('.'.$folder,0777);//创建年+月的文件夹权限为最高
		}
        $file_name = $this->GetRandStr();
		// $info = move_uploaded_file($file['tmp_name'], '.'.$folder.'/'.$file['name']); //移动文件到指定目录 建议文件名自己在修改一下
		$info = move_uploaded_file($file['tmp_name'], '.'.$folder.'/'.$file_name.".".$extension); //移动文件到指定目录 建议文件名自己在修改一下
		$img = $folder.'/'.$file_name.".".$extension;
		// exit(json_encode(array('code'=>0, 'msg'=>$img)));
        $res=['imgurl'=>$img];
        $ArrDate = ['code' => 0, 'data' => $res, 'token' => $request->input('token')];
        return BaseFun::jsonReturn($ArrDate);
	}

    public function GetRandStr($length=6){
        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $len = strlen($str)-1;
        $randstr = '';
        for ($i=0;$i<$length;$i++) {
            $num=mt_rand(0,$len);
            $randstr .= $str[$num];
        }
        return $randstr;
    }
}